<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><TITLE>for manual page - Built-In Commands</TITLE>
<link rel="stylesheet" href="../docs.css" type="text/css" media="all">
</HEAD>
<BODY><H2><a href="../contents.htm">Tcl8.6.11/Tk8.6.11 Documentation</a> <small>&gt;</small> <a href="contents.htm">Tcl Commands</a> <small>&gt;</small> for</H2>
<H3><A HREF="../UserCmd/contents.htm">Tcl/Tk Applications</A> | <A HREF="../TclCmd/contents.htm">Tcl Commands</A> | <A HREF="../TkCmd/contents.htm">Tk Commands</A> | <A HREF="../ItclCmd/contents.htm">[incr Tcl] Package Commands</A> | <A HREF="../SqliteCmd/contents.htm">SQLite3 Package Commands</A> | <A HREF="../TdbcCmd/contents.htm">TDBC Package Commands</A> | <A HREF="../TdbcmysqlCmd/contents.htm">tdbc::mysql Package Commands</A> | <A HREF="../TdbcodbcCmd/contents.htm">tdbc::odbc Package Commands</A> | <A HREF="../TdbcpostgresCmd/contents.htm">tdbc::postgres Package Commands</A> | <A HREF="../TdbcsqliteCmd/contents.htm">tdbc::sqlite3 Package Commands</A> | <A HREF="../ThreadCmd/contents.htm">Thread Package Commands</A> | <A HREF="../TclLib/contents.htm">Tcl C API</A> | <A HREF="../TkLib/contents.htm">Tk C API</A> | <A HREF="../ItclLib/contents.htm">[incr Tcl] Package C API</A> | <A HREF="../TdbcLib/contents.htm">TDBC Package C API</A></H3>
<H3><A NAME="M2">NAME</A></H3>
for &mdash; 'For' loop
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>for </B><I>start test next body</I><BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
<B>For</B> is a looping command, similar in structure to the C
<B>for</B> statement.  The <I>start</I>, <I>next</I>, and
<I>body</I> arguments must be Tcl command strings, and <I>test</I>
is an expression string.
The <B>for</B> command first invokes the Tcl interpreter to
execute <I>start</I>.  Then it repeatedly evaluates <I>test</I> as
an expression; if the result is non-zero it invokes the Tcl
interpreter on <I>body</I>, then invokes the Tcl interpreter on <I>next</I>,
then repeats the loop.  The command terminates when <I>test</I> evaluates
to 0.  If a <B><A HREF="../TclCmd/continue.htm">continue</A></B> command is invoked within <I>body</I> then
any remaining commands in the current execution of <I>body</I> are skipped;
processing continues by invoking the Tcl interpreter on <I>next</I>, then
evaluating <I>test</I>, and so on.  If a <B><A HREF="../TclCmd/break.htm">break</A></B> command is invoked
within <I>body</I>
or <I>next</I>,
then the <B>for</B> command will
return immediately.
The operation of <B><A HREF="../TclCmd/break.htm">break</A></B> and <B><A HREF="../TclCmd/continue.htm">continue</A></B> are similar to the
corresponding statements in C.
<B>For</B> returns an empty string.
<P>
Note: <I>test</I> should almost always be enclosed in braces.  If not,
variable substitutions will be made before the <B>for</B>
command starts executing, which means that variable changes
made by the loop body will not be considered in the expression.
This is likely to result in an infinite loop.  If <I>test</I> is
enclosed in braces, variable substitutions are delayed until the
expression is evaluated (before
each loop iteration), so changes in the variables will be visible.
See below for an example:
<H3><A NAME="M5">EXAMPLES</A></H3>
Print a line for each of the integers from 0 to 9:
<P>
<PRE><B>for</B> {set x 0} {$x&lt;10} {incr x} {
    puts &quot;x is $x&quot;
}</PRE>
<P>
Either loop infinitely or not at all because the expression being
evaluated is actually the constant, or even generate an error!  The
actual behaviour will depend on whether the variable <I>x</I> exists
before the <B>for</B> command is run and whether its value is a value
that is less than or greater than/equal to ten, and this is because
the expression will be substituted before the <B>for</B> command is
executed.
<P>
<PRE><B>for</B> {set x 0} $x&lt;10 {incr x} {
    puts &quot;x is $x&quot;
}</PRE>
<P>
Print out the powers of two from 1 to 1024:
<P>
<PRE><B>for</B> {set x 1} {$x&lt;=1024} {set x [expr {$x * 2}]} {
    puts &quot;x is $x&quot;
}</PRE>
<H3><A NAME="M6">SEE ALSO</A></H3>
<B><A HREF="../TclCmd/break.htm">break</A></B>, <B><A HREF="../TclCmd/continue.htm">continue</A></B>, <B><A HREF="../TclCmd/foreach.htm">foreach</A></B>, <B><A HREF="../TclCmd/while.htm">while</A></B>
<H3><A NAME="M7">KEYWORDS</A></H3>
<A href="../Keywords/B.htm#boolean">boolean</A>, <A href="../Keywords/F.htm#for">for</A>, <A href="../Keywords/I.htm#iteration">iteration</A>, <A href="../Keywords/L.htm#loop">loop</A>
<div class="copy">Copyright &copy; 1993 The Regents of the University of California.
<BR>Copyright &copy; 1994-1997 Sun Microsystems, Inc.
</div>
</BODY></HTML>
